package v2;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.PriorityScheduler;
import vo.PinPaiVO;

public class JdPinPaiUrlListPageProcessor implements PageProcessor{
	private Site site = Site.me()
			.setUserAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36")
			.setSleepTime(1000).setRetryTimes(2).setCycleRetryTimes(1000);
	
	private Map<String,PinPaiVO> pinPaiMap;
	
	public Map<String, PinPaiVO> getPinPaiMap() {
		String url = "http://list.jd.com/list.html?cat=9987,653,655&go=0&md=1&my=list_brand";
		long startTime = new Date().getTime();
		Spider spider = Spider.create(this).scheduler(new PriorityScheduler());
		spider.addUrl(url);
		spider.thread(3).run();
		
		long endTime = new Date().getTime();
		System.out.println("时间："+String.valueOf((endTime-startTime)));
		return this.pinPaiMap;
	}


	public void setPinPaiMap(Map<String, PinPaiVO> pinPaiMap) {
		this.pinPaiMap = pinPaiMap;
	}


	@Override
	public void process(Page page) {
		String url = page.getUrl().get();
		if("http://list.jd.com/list.html?cat=9987,653,655&go=0&md=1&my=list_brand".equals(url)){
			//添加所有手机品牌的入口url
			getPinPaiUrls(page);
		}
	}
	
	private void getPinPaiUrls(Page page){
		this.pinPaiMap = new HashMap<>();
		
		String pinpaiJsonStr = page.getJson().toString();
		JSONObject pinpaiJsonObj = JSONObject.parseObject(pinpaiJsonStr);
		JSONArray pinpaiJsonArray = JSONArray.parseArray(pinpaiJsonObj.getString("brands"));
		if(pinpaiJsonArray != null && pinpaiJsonArray.size()>0){
			for(int i=0;i<pinpaiJsonArray.size();i++){
				String jsonStr = pinpaiJsonArray.get(i).toString();
				JSONObject jsonObj = JSONObject.parseObject(jsonStr);
				String id = jsonObj.getString("id");
				String name = jsonObj.getString("name");
				
				if(id != null && name != null){
					String url = "http://list.jd.com/list.html?cat=9987,653,655&ev=exbrand_"+id+"&go=0&stock=0";
					PinPaiVO jdPinPaiVO = new PinPaiVO();
					jdPinPaiVO.setId(id);
					jdPinPaiVO.setName(name);
					jdPinPaiVO.setUrl(url);
					this.pinPaiMap.put(id, jdPinPaiVO);
				}
			}
		}
	}
	
	@Override
	public Site getSite() {
		return site;
	}
	
	public static void main(String[] args) {
		Map<String, PinPaiVO> pinpais = new JdPinPaiUrlListPageProcessor().getPinPaiMap();
		for(Map.Entry<String, PinPaiVO> entry : pinpais.entrySet()){
			System.out.println(entry.getValue().getName());
		}
	}
}
